package fr.obsmip.sedoo.server.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import fr.obsmip.sedoo.client.service.SearchService;
import fr.obsmip.sedoo.core.dao.GeonetworkUserDAO;
import fr.obsmip.sedoo.server.service.dtotool.MetadataDTOTools;
import fr.obsmip.sedoo.server.service.dtotool.SearchCriteriaDTOTools;
import fr.obsmip.sedoo.shared.domain.SearchCriteriaDTO;
import fr.obsmip.sedoo.shared.domain.SummaryDTO;
import fr.sedoo.commons.metadata.utils.domain.Summary;
import fr.sedoo.rbv.geonetwork.request.SearchRequest;

public class SearchServiceImpl extends RemoteServiceServlet implements SearchService
{

	@Override
	public ArrayList<SummaryDTO> getSummaries(SearchCriteriaDTO criteria, int position, int size, ArrayList<String> displayLanguages)
			throws Exception {
		
		SearchCriteriaDTO aux = criteria;
		if (aux == null)
		{
			aux = new SearchCriteriaDTO();
		}
		SearchRequest request;
		if (StringUtils.isEmpty(aux.getObservatory()) == false)
		{
			//On filtre par observatoire en utilisant uniquement l'utilisateur correspondant
			request = new SearchRequest(GeonetworkUserDAO.getUserFromObservatoryShortLabel(aux.getObservatory()));
		}
		else
		{
			// On utilise l'utilisateur d'administration pour effectuer la requête
			request = new SearchRequest();
		}
		request.setCriteria(SearchCriteriaDTOTools.fromDTO(aux));
		request.setPagePosition(position);
		request.setPageSize(size);
		request.setDisplayLanguages(displayLanguages);
		boolean isCorrectlyExecuted = request.fetchSummaries();
		if (isCorrectlyExecuted == false)
		{
			throw new Exception("Uncorrect execution");
		}
		else
		{
			ArrayList<SummaryDTO> result = new ArrayList<SummaryDTO>();
			List<Summary> summaries = request.getSummaries();
			if (summaries != null)
			{
				Iterator<Summary> iterator = summaries.iterator();
				while (iterator.hasNext()) {
					Summary summary = iterator.next();
					result.add(MetadataDTOTools.toSummaryDTO(summary, displayLanguages));
				}
			}
			return result;
		}
	}

	@Override
	public int getHits(SearchCriteriaDTO criteria) throws Exception {
		
		SearchCriteriaDTO aux = criteria;
		if (aux == null)
		{
			aux = new SearchCriteriaDTO();
		}
		SearchRequest request;
		if (StringUtils.isEmpty(aux.getObservatory()) == false)
		{
			//On filtre par observatoire en utilisant uniquement l'utilisateur correspondant
			request = new SearchRequest(GeonetworkUserDAO.getUserFromObservatoryShortLabel(aux.getObservatory()));
		}
		else
		{
			// On utilise l'utilisateur d'administration pour effectuer la requête
			request = new SearchRequest();
		}
		request.setCriteria(SearchCriteriaDTOTools.fromDTO(criteria));
		boolean isCorrectlyExecuted = request.calculateHits();
		if (isCorrectlyExecuted == false)
		{
			throw new Exception("Uncorrect execution");
		}
		else
		{
			return request.getHits();
		}
	}

		
}

